<?php

// phpMyRealty 3
//
// File Name: advsearchform.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include configuration file and general functions
define('PMR', 'true');

$page = 'search';

include ( './config.php' );
include ( PATH . '/defaults.php' );

// ----------------------------------------------------------------------
// ADVANCED SEARCH SECTION

// Title tag content
$title = $conf['website_name_short'] . ' - ' . $lang['Property_Search'] . ' / ' . $lang['Realtor_Search'];

$initAjax = TRUE;

if (isset($initAjax) && $initAjax)
 include ( PATH . '/includes/ajaxDropdowns.php');

// Template header
include ( PATH . '/templates/' . $cookie_template . '/header.php' );

// Output the Find Property form
echo table_header ( $lang['Property_Search'] );

echo '
<form action="' . URL . '/search.php" method="POST" name="form">
 <table width="100%" cellpadding="5" cellspacing="0" border="0">
     ';

echo '<input type="hidden" name="seed" value="' . rand(1,1000) . '">';

echo userform ($lang['Search_Images_Only'], '<input type="checkbox" name="image_uploaded" value="1">');

// 'Sort By' options list (db field name, variable name)
$order_options = '';
if (strcasecmp($conf['show_mls'], 'OFF')) {
	$order_options .= '<option value="mls">' . $lang['Listing_MLS'] . '</option>';
}
$order_options .= '<option value="title">' . $lang['Listing_Title'] . '</option>
<option value="price">' . $lang['Listing_Price'] . '</option>
<option value="bathrooms">' . $lang['Listing_Bathrooms'] . '</option>
<option value="bedrooms">' . $lang['Listing_Bedrooms'] . '</option>';
if (strcasecmp($conf['show_postal_code'], 'OFF')) {
	$order_options .= '<option value="zip">' . $lang['Zip_Code'] . '</option>';
}
$order_options .= '
<option value="year_built">' . $lang['Listing_Year_Built'] . '</option>
<option value="date_added">' . $lang['Date_Added'] . '</option>
<option value="date_updated">' . $lang['Date_Updated'] . '</option>

	 	 ';

// 'Sort By' ASC DESC options
$order_type_options = '

<option value="ASC">' . $lang['Listing_Ascending'] . '</option>
<option value="DESC">' . $lang['Listing_Descending'] . '</option>

		      ';

// General Find Listing form
echo userform ($lang['Order_By'], '<select name="order_by"><option value="ANY">' . $lang['Search_Any'] . '</option>' . $order_options . '</select> <select name="order_by_type">' . $order_type_options . '</select>');
echo userform ($lang['Module_Listing_Type'], '<select name="type2"><option value="ANY" class="price_range_any">' . $lang['Search_Any'] . '</option>' . generate_options_list(TYPES2_TABLE, '', 'price_range_') . '</select>');
echo userform ($lang['Listing_Status'], '<select name="status"><option value="ANY">' . $lang['Search_Any'] . '</option>' . generate_options_list(STATUS_TABLE) . '</select>');
if (strcasecmp($conf['show_mls'], 'OFF')) 
    echo userform ($lang['Listing_MLS'], '<input type="text" size="45" name="mls" maxlength="46">');
echo userform ($lang['Listing_Title'], '<input type="text" size="45" name="title" maxlength="255">');
echo userform ($lang['Listing_Property_Type'], '<select name="type"><option value="ANY">' . $lang['Search_Any'] . '</option>' . generate_options_list(TYPES_TABLE) . '</select>');
echo userform ($lang['Listing_Style'], '<select name="style"><option value="ANY">' . $lang['Search_Any'] . '</option>' . generate_options_list(STYLES_TABLE) . '</select>');
echo userform ($lang['Search_Keyword'], '<input type="text" size="45" name="keyword" maxlength="50">');
echo userform ($lang['Listing_Lot_Size'], '<input type="text" size="45" name="size" maxlength="50">');
echo userform ($lang['Listing_Dimensions'], '<input type="text" size="45" name="dimensions" maxlength="50">');
echo userform ($lang['Listing_Bathrooms'], '
		<select name="bathrooms">
		 <option value="ANY">' . $lang['Search_Any'] . '</option>
		 <option value="1">1+</option>
		 <option value="2">2+</option>
		 <option value="3">3+</option>
		 <option value="4">4+</option>
		 <option value="5">5+</option>
		 <option value="6">6+</option>
		 <option value="7">7+</option>
		 <option value="8">8+</option>
		 <option value="9">9+</option>
		 <option value="10">10+</option>
		</select>
');

echo userform ($lang['Listing_Half_Bathrooms'], '
		<select name="half_bathrooms">
		 <option value="ANY">' . $lang['Search_Any'] . '</option>
		 <option value="1">1+</option>
		 <option value="2">2+</option>
		 <option value="3">3+</option>
		 <option value="4">4+</option>
		 <option value="5">5+</option>
		 <option value="6">6+</option>
		 <option value="7">7+</option>
		 <option value="8">8+</option>
		 <option value="9">9+</option>
		 <option value="10">10+</option>
		</select>
');

echo userform ($lang['Listing_Bedrooms'], '
		<select name="bedrooms">
		 <option value="ANY">' . $lang['Search_Any'] . '</option>
		 <option value="1">1+</option>
		 <option value="2">2+</option>
		 <option value="3">3+</option>
		 <option value="4">4+</option>
		 <option value="5">5+</option>
		 <option value="6">6+</option>
		 <option value="7">7+</option>
		 <option value="8">8+</option>
		 <option value="9">9+</option>
		 <option value="10">10+</option>
		</select>
');

// ==

  $r_location1 = $db->query('SELECT selector, category FROM ' . LOCATION1_TABLE . ' ORDER BY category ASC');

  $newContentMain = '<option value="ANY">' . $lang['Select'] . '</option>' . "\n";
   
  while ($f_location1 = $db->fetcharray($r_location1)) {
   $newContentMain.= '<option value="' . $f_location1['selector'] . '">' . $f_location1['category'] . '</option>' . "/n";
  }

  echo userform ($lang['Location'], '
          <select name="location1" class="span3" id="location1" onchange="xajax_fetchLocations2(this.value);">

' . $newContentMain . '

          </select>
          <div style="display: inline" id="location2div" name="location2div"></div>
          <div style="display: inline" id="location3div" name="location3div"></div>');

// ==

if (strcasecmp(@$conf['show_postal_code'], 'OFF') != 0) {
  echo userform ($lang['Zip_Code'], '<input type="text" size="45" name="zip" maxlength="20">');

  echo userform ($lang['Your_Zip_Code'], '<input type="text" size="7" name="your_zip" maxlength="7"> ' . $lang['Your_Zip_Code_Radius'] .
	       '<select name="miles">
		 <option value="1">1</option>
		 <option value="2">2</option>
		 <option value="5">5</option>
		 <option value="10">10</option>
		 <option value="20">20</option>
		 <option value="50">50</option>
		</select>');
}

echo userform ($lang['Realtor_Address'], '<input type="text" size="45" name="address" maxlength="50">');
echo userform ($lang['Listing_Directions'], '<input type="text" size="45" name="directions" maxlength="50">');
echo userform ($lang['Listing_Year_Built'], '<input type="text" size="45" name="year_built" maxlength="4">');
echo userform ($lang['Listing_Additional_Out_Buildings'], generate_checkbox_list(BUILDINGS_TABLE, 'buildings'));
echo userform ($lang['Listing_Appliances_Included'], generate_checkbox_list(APPLIANCES_TABLE, 'appliances'));
echo userform ($lang['Listing_Features'], generate_checkbox_list(FEATURES_TABLE, 'features'));
echo userform ($lang['Listing_Garage'], '<select name="garage"><option value="ANY">' . $lang['Search_Any'] . '</option>' . generate_options_list(GARAGE_TABLE) . '</select>');
echo userform ($lang['Listing_Garage_Cars'], '<input type="text" size="45" name="garage_cars" maxlength="2">');
echo userform ($lang['Listing_Basement'], '<select name="basement"><option value="ANY">' . $lang['Search_Any'] . '</option>' . generate_options_list(BASEMENT_TABLE) . '</select>');

// Custom fields
$query = "SELECT * FROM " . FIELDS_TABLE . " ORDER BY name ASC";
$result = $db->query($query) OR error( 'Critical Error:' . $query);
if ($db->numrows($result) > 0) {
	while($row = $db->fetcharray($result)) {
		// Type of input
		if ($row['type'] != '') {
			if ($row['type'] == 'input') {
				echo userform ($row['name'], '<input type="text" name="' . $row['field'] . '">');
			} elseif ($row['type'] == 'select') {
				// Grab all options for this select
				$options = "SELECT * FROM " . VALUES_TABLE . " WHERE f_id = '" . addslashes($row['id']) . "'";
				$get_options = $db->query($options) OR error( 'Critical Error:' . $options);
				if ($db->numrows($get_options) > 0) {
					$option_list = '<option value="">Any</option>';
					while($row2 = $db->fetcharray($get_options)) {
						$option_list .= "<option value=\"" . $row2['id'] . "\">" . $row2['name'] . "</option>";
					}
					echo userform ($row['name'], '<select name="' . $row['field'] . '">' . $option_list . '</select>');
				}
			}
		}
	}
}

// Price range, outputs the values according to the configuration settings
// price_range_min TO price_range_max with the step of price_range_step

$price_ranges = array();
$price_ranges['monthly'] = array();
for ($i = $conf['price_monthly_range_min']; $i <= $conf['price_monthly_range_max']; $i = $i + $conf['price_monthly_range_step']) {
    $price_ranges['monthly'][] = $i;
}
$price_ranges['sale'] = array();
for ($i = $conf['price_range_min']; $i <= $conf['price_range_max']; $i = $i + $conf['price_range_step']) {
    $price_ranges['sale'][] = $i;
}
$price_ranges['any'] = array_unique(array_merge($price_ranges['monthly'], $price_ranges['sale']));
sort($price_ranges['any']);

foreach ($price_ranges as $price_type => $prices) {
    $price_range = '';
    foreach ($prices as $i) {
        $price_range.= '<option value="' . $i . '">' . $conf['currency'] . ' '
                       . pmr_number_format($i) . '</option>' . "\n";
    }
    switch ($price_type) {
    case 'any':
        $_style = '';
        break;
    default:
        $_style = ' style="display: none;"';
        break;
    }

    echo '<tr id="price_range_'.$price_type.'" class="price_range" '.$_style.'>
            <td align="right">'.$lang['Search_Price_Range'].'</td>
            <td><select name="price_min_'.$price_type.'">
    	            <option value="ANY">' . $lang['Search_Any'] . '</option>
                    ' . $price_range . '
                </select> - 
                <select name="price_max_'.$price_type.'">
    	            <option value="ANY">' . $lang['Search_Any'] . '</option>
                ' . $price_range . '
                </select>
            </td>
         </tr>
         ';
}

?>
<script type="text/javascript">
(function($){
    $(document).ready(function() {
        $(":select[name=type2]").change(function() {
            var c = $(":select[name=type2] option:selected").attr("class");
            $(".price_range").hide();
            $("#" + c).show();
        });
        $(":select[name=type2]").change();
    });
})(jQuery);
</script>
<?php 

// Submit button
echo userform ('', '<input type="Submit" class="submit" name="property_search" value="' . $lang['Property_Search'] . '">');

echo '
 </table>
</form>
     ';

echo table_footer ();

// Output the Find a Realtor form
echo table_header ( $lang['Realtor_Search'] );

echo '
<form action="' . URL . '/usersearch.php" method="POST">
 <table width="100%" cellpadding="5" cellspacing="0" border="0">
     ';

echo userform ($lang['Search_Images_Only'], '<input type="checkbox" name="image_uploaded" value="1">');
echo userform ($lang['Realtor_First_Name'], '<input type="text" size="45" name="realtor_first_name" maxlength="255">');
echo userform ($lang['Realtor_Last_Name'], '<input type="text" size="45" name="realtor_last_name" maxlength="255">');
echo userform ($lang['Realtor_Company_Name'], '<input type="text" size="45" name="realtor_company_name" maxlength="255">');
echo userform ($lang['Search_Keyword'], '<input type="text" size="45" name="realtor_description" maxlength="100">');
echo userform ($lang['Location'], '<select name="realtor_location"><option value="ANY" SELECTED>' . $lang['Search_Any'] . ' ' . $lang['Location'] . '</option>' . generate_options_list(LOCATIONS_TABLE) . '</select>');
echo userform ($lang['City'], '<input type="text" size="45" name="realtor_city" maxlength="50">');
echo userform ($lang['Realtor_Address'], '<input type="text" size="45" name="realtor_address" maxlength="255">');
if (strcasecmp(@$conf['show_postal_code'], 'OFF'))
    echo userform ($lang['Zip_Code'], '<input type="text" size="45" name="realtor_zip_code" maxlength="20">');
echo userform ($lang['Realtor_Phone'], '<input type="text" size="45" name="realtor_phone" maxlength="50">');
echo userform ($lang['Realtor_Fax'], '<input type="text" size="45" name="realtor_fax" maxlength="50">');
echo userform ($lang['Realtor_Mobile'], '<input type="text" size="45" name="realtor_mobile" maxlength="50">');

echo userform ($lang['Realtor_e_mail'], '<input type="text" size="45" name="realtor_e_mail" maxlength="50">');
echo userform ($lang['Realtor_Website'], '<input type="text" size="45" name="realtor_website" maxlength="255">');
echo userform ($lang['Realtor_Login'], '<input type="text" size="45" name="realtor_login" maxlength="50">');

// Submit button
echo userform ('', '<input type="Submit" class="submit" name="realtor_search" value="' . $lang['Realtor_Search'] . '">');

echo '
 </table>
</form>
';

echo table_footer ();

// Template Footer
include ( PATH . '/templates/' . $cookie_template . '/footer.php' );

?>